package tank.draw;

import java.util.Random;
import java.util.Vector;

/**
 * @Author: WaveKing
 * @Description: TODO
 * @Date: Created in 19:12 2023/11/16
 * @Modified By:
 */
public class enemyTank extends tank implements Runnable{
    Vector<bullet> bullets = new Vector<>();
    public enemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {
        while (true){
            //
            bullet b = null;
            if (bullets.size()<3){
                switch (getDirect()){
                    case 0:
                        b = new bullet(getX()+20,getY(),0);
                        try {
                            Thread.sleep(1000 );
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        break;
                    case 1:
                        b = new bullet(getX()+60,getY()+20,1);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        break;
                    case 2:
                        b = new bullet(getX()+20,getY()+60,2);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        break;
                    case 3:
                        b = new bullet(getX(),getY()+20,3);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        break;
                }

                bullets.add(b);
                new Thread(b).start();
            }

            //根据坦克方向移动，并随机改变
            switch (getDirect()){
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if (getY()>0){
                            up();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                  break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        if (getX()+60<1000){
                            right();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        if (getY()+60<750){
                            down();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if (getX()>0){
                            left();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            //随机改变坦克方向
            setDirect((int) (Math.random()*4));
            if (!isLive){
                break;
            }
        }
    }
}
